﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Forget.Data;

namespace Forget.HRBLL.Model
{
    public partial class Department
    {
        public static readonly string ADDDEPARTMENT = "HR_AddDepartment";
        public static readonly string EDITDEPARTMENT = "HR_EditDepartment";
        public static readonly string DELDEPARTMENT = "HR_DelDepartment";

        #region properties
        [DataParam(32, SqlDbType.Int)]
        public int? DepartmentID { get; set; }

        [DataParam(20, SqlDbType.NVarChar)]
        public string Name { get; set; }

        [DataParam(32, SqlDbType.Int)]
        public int? ParentID { get; set; }

        [DataParam(32, SqlDbType.Int)]
        public int? Level { get; set; }

        [DataParam(0, SqlDbType.DateTime)]
        public DateTime CreateTime { get; set; }
        #endregion


        /// <summary>
        /// 添加新的部门，只能存在一个parentID=0，0代表公司最高部门
        /// </summary>
        /// <param name="name">部门名称</param>
        /// <param name="parentID">部门所属</param>
        /// <returns></returns>
        public static bool AddDepartment(Department dp)
        {

            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(dp);
            try
            {
                if (DataAccess.ExecuteNonQuery(CommandType.StoredProcedure, ADDDEPARTMENT, parms.ToArray()) > 0)
                    return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.AddDepart", ex.Message, ex.StackTrace);
            }

            return false;
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public static bool EditDepartment(Department dp)
        {

            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(dp);
            try
            {
                if (DataAccess.ExecuteNonQuery(CommandType.StoredProcedure, EDITDEPARTMENT, parms.ToArray()) > 0)
                    return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.EditDepart", ex.Message, ex.StackTrace);
            }

            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="departmentID"></param>
        /// <returns>0代表成功，1代表department为0，2代表此部门下有子部门，3代表此部门下有员工</returns>
        public static bool DelDepartment(Department dp)
        {            
            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(dp);
            try
            {
                if (DataAccess.ExecuteNonQuery(CommandType.StoredProcedure, DELDEPARTMENT, parms.ToArray()) > 0)
                    return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.DelDepart", ex.Message, ex.StackTrace);
            }
            return false;
        }       
    }
}
